/**
 * fileName:com.zimo.web.util.CloudUtil.java
 * projectName:Zimo
 */
package com.zimo.web.util;

import java.io.InputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.baidu.inf.iis.bcs.BaiduBCS;
import com.baidu.inf.iis.bcs.auth.BCSSignCondition;
import com.baidu.inf.iis.bcs.http.HttpMethodName;
import com.baidu.inf.iis.bcs.model.BucketSummary;
import com.baidu.inf.iis.bcs.model.ObjectMetadata;
import com.baidu.inf.iis.bcs.model.X_BS_ACL;
import com.baidu.inf.iis.bcs.request.CreateBucketRequest;
import com.baidu.inf.iis.bcs.request.GenerateUrlRequest;
import com.baidu.inf.iis.bcs.request.ListBucketRequest;
import com.baidu.inf.iis.bcs.request.PutObjectRequest;
import com.baidu.inf.iis.bcs.response.BaiduBCSResponse;

/**
 * 云存储工具类
 * @author sg.z/2013-12-23/下午4:56:56
 */
public class CloudUtil {
	private static final Log log = LogFactory.getLog(CloudUtil.class);
	
	/**
	 * 
	 * description :  获取bucket列表 
	 * @return List<BucketSummary>
	 * @param baiduBCS
	 * @return
	 * @author sg/2013-12-23/下午4:58:29
	 */
	public static List<BucketSummary> listBucket(BaiduBCS baiduBCS) {
		ListBucketRequest listBucketRequest = new ListBucketRequest();
		BaiduBCSResponse<List<BucketSummary>> response = baiduBCS.listBucket(listBucketRequest);
		return response.getResult();
	}
	
	/**
	 * 
	 * description :   创建bucket
	 * @return void
	 * @param baiduBCS
	 * @author sg/2013-12-23/下午5:02:20
	 */
	public static void createBucket(BaiduBCS baiduBCS,String bucket) {
		baiduBCS.createBucket(new CreateBucketRequest(bucket, X_BS_ACL.PublicWrite));
	}
	
	public static void putObjectByInputStream(BaiduBCS baiduBCS,InputStream fileContent,String bucket,String object,String contentType,long fileLength) {
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentType(contentType);
		objectMetadata.setContentLength(fileLength);
		PutObjectRequest putrequest = new PutObjectRequest(bucket, object, fileContent, objectMetadata);
		ObjectMetadata objresult = baiduBCS.putObject(putrequest).getResult();
		log.info(objresult);
	}
	
	/**
	 * 获取上传资源的url地址
	 * @return String
	 * @param baiduBCS BaiduBCS对象
	 * @param bucket 上传选择的bucket
	 * @param object 上传的具体资源目录，相对bucket的
	 * @param ip 限制的ip 可以为空
	 * @param time 时间 可以为空
	 * @param size 大小 可以为空
	 * @return
	 * @author sg/2013-12-25/上午11:46:02
	 */
	public static String generateUrl(BaiduBCS baiduBCS,String bucket,String object,String ip,long time,long size) {
		GenerateUrlRequest generateUrlRequest = new GenerateUrlRequest(HttpMethodName.GET, bucket, object);
		generateUrlRequest.setBcsSignCondition(new BCSSignCondition());
		if(StringUtils.isNotBlank(ip)){
			generateUrlRequest.getBcsSignCondition().setIp(ip.trim());
		}
		if(time>0){
			generateUrlRequest.getBcsSignCondition().setTime(time);
		}
		if(size>0){
			generateUrlRequest.getBcsSignCondition().setSize(size);
		}
		return baiduBCS.generateUrl(generateUrlRequest);
	}
	
}
